package com.baidu.mapsdkexample.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapsdkexample.BMapApiDemoMain;
import com.baidu.mapsdkexample.R;
import com.baidu.mapsdkexample.routeplan.TransitRoutePlanActivity;

import cn.leancloud.AVLogger;
import cn.leancloud.AVOSCloud;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class LoginActivity extends AppCompatActivity {

    private Button register,login;
    private EditText username;
    private EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        register = findViewById(R.id.register);
        login=findViewById(R.id.login);
        username = findViewById(R.id.accountEdit);
        password = findViewById(R.id.passwordEdit);
        AVOSCloud.setLogLevel(AVLogger.Level.DEBUG);
        AVOSCloud.initialize(
                this,
                "gYKKKJUqrlSsLCUDRD1aNRHj-gzGzoHsz",
                "viezs5eeEG4dLaPay20EaEXd",
                "dwxr3lun.api.lncld.net"
        );
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 创建实例
                AVUser user = new AVUser();

                // 等同于 user.put("username", "Tom")
                user.setUsername(username.getText().toString());
                user.setPassword(password.getText().toString());

                // 可选
                //user.setEmail("tom@leancloud.rocks");
                //user.setMobilePhoneNumber("+8618200008888");

                // 设置其他属性的方法跟 AVObject 一样
                user.put("gender", "secret");

                user.signUpInBackground().subscribe(new Observer<AVUser>() {
                    public void onSubscribe(Disposable disposable) {
                    }

                    public void onNext(AVUser user) {
                        // 注册成功
                        Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        System.out.println("------------------------------注册成功。objectId：" + user.getObjectId());
                    }

                    public void onError(Throwable throwable) {
                        // 注册失败（通常是因为用户名已被使用）
                        Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        System.out.println("---------------------------------------注册失败");
                    }

                    public void onComplete() {
                    }
                });
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AVUser.logIn(username.getText().toString(), password.getText().toString()).subscribe(new Observer<AVUser>() {
                    public void onSubscribe(Disposable disposable) {}
                    public void onNext(AVUser user) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        System.out.println("---------------------------------------登录成功");
                        Intent intent=new Intent(LoginActivity.this, BMapApiDemoMain.class);
                        startActivity(intent);
                        // 登录成功
                    }
                    public void onError(Throwable throwable) {
                        Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                        System.out.println("---------------------------------------登录失败");
                        // 登录失败（可能是密码错误）
                    }
                    public void onComplete() {}
                });
            }
        });
    }
}